package cn.xuewenbao.eduorder.controller;


import cn.xuewenbao.edu.service.CourseService;
import cn.xuewenbao.eduorder.entity.Eduorder;
import cn.xuewenbao.eduorder.entity.vo.OrderInfoVo;
import cn.xuewenbao.eduorder.service.EduorderService;
import cn.xuewenbao.ucenter.service.MemberService;
import cn.xuewenbao.ucenter.service.VipService;
import cn.xuewenbao.ucenter.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author mafayu
 * @since 2021-06-30
 */
@RestController
@RequestMapping("/eduorder/order")
@Api(tags = "订单表")
public class EduorderController {

    @Autowired
    private EduorderService eduorderService;
    @Autowired
    private VipService vipService;
    @Autowired
    private CourseService courseService;


    //生成课程订单
    @ApiOperation(value = "生成课程订单")
    @PostMapping("createCourseOrder/{courseId}")
    public Result<String> createCourseOrder(@PathVariable String courseId, HttpServletRequest request) {
        //创建订单，返回订单号
        Result<String> result = eduorderService.createCourseOrder(courseId, JwtUtils.getMobileByToken(request));
        return result;
    }

    //生成会员订单
    @ApiOperation(value = "生成会员订单")
    @PostMapping("createVipOrder/{vipTypeId}")
    public Result<String> createVipOrder(@PathVariable String vipTypeId, HttpServletRequest request) {
        //创建订单，返回订单号
        return eduorderService.createVipOrder(vipTypeId, JwtUtils.getMobileByToken(request));
    }

    //根据订单id查询订单信息
    @ApiOperation(value = "根据订单id查询订单信息")
    @PostMapping("getOrderInfo")
    public Result<OrderInfoVo> getOrderInfo(@RequestBody Map<String, Object> map) {
        String orderId = (String) map.get("orderId");
        Result<OrderInfoVo> result = new Result<>();
        QueryWrapper<Eduorder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderId);
        Eduorder order = eduorderService.getOne(wrapper);
        if (order == null) {
            result.setMessage("订单为空");
            result.setSuccess(false);
            return result;
        }
        OrderInfoVo vo = new OrderInfoVo();
        BeanUtils.copyProperties(order, vo);
        String description;
        if (order.getColumnType() == null) {    //columnType为空, 该订单为开通会员的订单
            description = vipService.getOrderDescription(order.getContentType(), order.getMemberId());
        } else {    //购买商品或服务的订单
            description = courseService.getOrderDescription(order.getContentId());
        }
        vo.setDescription(description);
        result.setResult(vo);
        result.setMessage("查询成功");
        result.setSuccess(true);
        return result;
    }

    //根据课程id和用户id查询订单表中订单状态
    @ApiOperation(value = "根据课程id和用户id查询订单表中订单状态")
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public Result<Boolean> isBuyCourse(@PathVariable String courseId, @PathVariable String memberId) {
        Result<Boolean> result = new Result<>();
        QueryWrapper<Eduorder> wrapper = new QueryWrapper<>();
        wrapper.eq("content_id", courseId);
        wrapper.eq("member_id", memberId);
        wrapper.eq("status", 1);//支付状态 1代表已经支付
        int count = eduorderService.count(wrapper);
        if (count > 0) { //已经支付
            result.setResult(true);
            result.setMessage("支付成功");
            result.setSuccess(true);
            return result;
        } else {
            result.setMessage("支付失败");
            result.setSuccess(false);
            return result;
        }
    }

    //获取我的已购内容列表(课程)
    @ApiOperation("获取我的已购专栏内容列表")
    @PostMapping("purchasedList")
    public Result<?> likeList(@RequestBody Map<String, String> map) {
        String userId = map.get("userId");
        String columnType = map.get("columnType");
        Result<?> result = eduorderService.getPurchasedList(userId, columnType);
        return result;
    }
}

